<?php

namespace app\common\services;

use app\utils\HttpUtil;

class SmsService extends BaseService{

    const SMS_URL = "https://api.mysubmail.com/internationalsms/send.json";

    /**
     * 发送短信
     * @param string $mobile 手机号
     * @param string $template 短信模板
     * @param array $keys 变量KEY
     * @param array $values 变量值
     * @return bool
     * @throws \yii\web\HttpException
     */
    public static function send($mobile,$template,$keys,$values){
        $content = str_replace($keys,$values,$template);
        $post_data = [
            'appid'=>\Yii::$app->params['sms']['appid'],
            'signature'=>\Yii::$app->params['sms']['appkey'],
            'to'=>\Yii::$app->params['sms']['country_code'].$mobile,
            'content'=>\Yii::$app->params['sms']['prefix'].$content,
        ];
        $result = HttpUtil::request(self::SMS_URL,HttpUtil::METHOD_POST,$post_data,HttpUtil::FORMAT_JSON);
        if($result['status'] != "success"){
            self::setError($result['msg']);
            \Yii::info("短信发送失败:".SmsService::getError());
            return false;
        }
        return true;
    }
}